package com.wlb365.financing.modules.esign.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.collect.Lists;
import com.wlb365.financing.common.dto.R;
import com.wlb365.financing.common.exception.AdminException;
import com.wlb365.financing.common.utils.ShiroUtil;
import com.wlb365.financing.common.utils.StringUtils;
import com.wlb365.financing.modules.esign.response.seals.QuerySealsPageRes;
import com.wlb365.financing.modules.esign.response.seals.SealsDTO;
import com.wlb365.financing.modules.esign.service.SealsService;
import com.wlb365.financing.modules.esign.vo.CreateSealVO;
import com.wlb365.financing.modules.esign.vo.SealsQueryVO;
import com.wlb365.financing.modules.sys.entity.Dept;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import sun.misc.BASE64Encoder;

/**
 * <p>
 * 概述：印章相关
 * </p>
 *
 * @author zwh
 * @version 1.0
 * @copyRight 北京微利宝信息技术有限公司  All right reserved.
 * @date 2021/5/27
 */
@Slf4j
@RestController
@RequestMapping("seals")
public class SealsController {
    @Autowired
    private SealsService sealsService;

    @RequiresPermissions("seals:person:index")
    @GetMapping(value = "person/index")
    public ModelAndView person() {
        return new ModelAndView("esign/seals/person");
    }

    @GetMapping(value = "person/list/data")
    public R personListData(SealsQueryVO queryVO) {
        //获取当前登录用户签章账号
        String accountId = ShiroUtil.getUser().getAccountId();
        if (StringUtils.isEmpty(accountId)) {
            Page<SealsDTO> page = new Page<>();
            page.setCurrent(queryVO.getCurrent());
            page.setTotal(0);
            page.setSize(queryVO.getSize());
            page.setRecords(Lists.newArrayList());
            return R.ok(page);
        }
        QuerySealsPageRes res = sealsService.page("0", accountId, queryVO.getCurrent(), queryVO.getSize());
        Page<SealsDTO> page = new Page<>();
        page.setCurrent(queryVO.getCurrent());
        page.setTotal(res.getTotal());
        page.setSize(queryVO.getSize());
        page.setRecords(res.getSeals());
        res.getSeals().stream().forEach(s -> {
            s.setAccountId(accountId);
        });
        return R.ok(page);
    }

    @RequiresPermissions("seals:person:edit")
    @GetMapping(value = "person/edit")
    public ModelAndView personAdd() {
        //获取当前登录用户签章账号
        String accountId = ShiroUtil.getUser().getAccountId();
        if (StringUtils.isEmpty(accountId)) {
           throw new AdminException("当前登录人未创建签章账号！请先创建签章账号");
        }
        ModelAndView mv = new ModelAndView("esign/seals/person_edit");
        SealsDTO dto = new SealsDTO();
        dto.setAccountId(accountId);
        mv.addObject("editInfo", dto);
        return mv;
    }

    @RequiresPermissions("seals:person:edit")
    @PostMapping(value = "person/edit")
    public R add(CreateSealVO vo) {
        System.out.println(vo.getAccountId());
        if ("1".equals(vo.getSealType())) {
            if (StringUtils.isEmpty(vo.getBase64())) {
                throw new AdminException("印章图片不能为空");
            }
            sealsService.image(vo.getAccountId(), "BASE64", vo.getBase64(), vo.getAlias(), true, null, null);
        } else {
            sealsService.personalTemplate(vo.getAccountId(), vo.getType(), vo.getAlias(), vo.getColor(), null, null);
        }
        return R.ok();
    }

    @RequiresPermissions("seals:person:delete")
    @PostMapping(value = "person/delete")
    public R personDelete(String accountId, String sealId) {
        sealsService.delete("0", accountId, sealId);
        return R.ok();
    }

    @RequiresPermissions("seals:person:default")
    @PostMapping(value = "person/default")
    public R personDefault(String accountId, String sealId) {
        sealsService.defaultSeal("0", accountId, sealId);
        return R.ok();
    }


    @RequiresPermissions("seals:official:index")
    @GetMapping(value = "official/index")
    public ModelAndView official() {
        return new ModelAndView("esign/seals/official");
    }

    @GetMapping(value = "official/list/data")
    public R officialListData(SealsQueryVO queryVO) {
        //获取当前登录机构签章账号
        Dept dept = ShiroUtil.getUser().getDept();
        if (dept == null || StringUtils.isEmpty(dept.getAccountId())) {
            Page<SealsDTO> page = new Page<>();
            page.setCurrent(queryVO.getCurrent());
            page.setTotal(0);
            page.setSize(queryVO.getSize());
            page.setRecords(Lists.newArrayList());
            return R.ok(page);
        }
        QuerySealsPageRes res = sealsService.page("1", dept.getAccountId(), queryVO.getCurrent(), queryVO.getSize());
        Page<SealsDTO> page = new Page<>();
        page.setCurrent(queryVO.getCurrent());
        page.setTotal(res.getTotal());
        page.setSize(queryVO.getSize());
        page.setRecords(res.getSeals());
        res.getSeals().stream().forEach(s -> {
            s.setAccountId(dept.getAccountId());
        });
        return R.ok(page);
    }

    @RequiresPermissions("seals:official:edit")
    @GetMapping(value = "official/edit")
    public ModelAndView officialAdd() {
        //获取当前登录机构签章账号
        Dept dept = ShiroUtil.getUser().getDept();
        if (dept == null || StringUtils.isEmpty(dept.getAccountId())) {
            throw new AdminException("当前登录人所属机构未创建签章账号！请先创建签章账号");
        }
        ModelAndView mv = new ModelAndView("esign/seals/official_edit");
        SealsDTO dto = new SealsDTO();
        dto.setAccountId(dept.getAccountId());
        mv.addObject("editInfo", dto);
        return mv;
    }

    @RequiresPermissions("seals:official:edit")
    @PostMapping(value = "official/edit")
    public R officialAdd(CreateSealVO vo) {
        System.out.println(vo.getAccountId());
        if ("1".equals(vo.getSealType())) {
            if (StringUtils.isEmpty(vo.getBase64())) {
                throw new AdminException("印章图片不能为空");
            }
            sealsService.image(vo.getAccountId(), "BASE64", vo.getBase64(), vo.getAlias(), true, null, null);
        } else {
            sealsService.createOfficialTemplate(vo.getAccountId(), vo.getType(), vo.getCentral(), vo.getAlias(), vo.getColor(), null, null, vo.getHtext(), vo.getQtext());
        }
        return R.ok();
    }

    @RequiresPermissions("seals:official:delete")
    @PostMapping(value = "official/delete")
    public R officialDelete(String accountId, String sealId) {
        sealsService.delete("1", accountId, sealId);
        return R.ok();
    }

    @RequiresPermissions("seals:official:default")
    @PostMapping(value = "official/default")
    public R officialDefault(String accountId, String sealId) {
        sealsService.defaultSeal("1", accountId, sealId);
        return R.ok();
    }


    @PostMapping(value = "getBase64")
    public R getBase64( @RequestParam("file") MultipartFile file) {
        return R.ok(base64(file));
    }
    private String base64(MultipartFile file) {
        try {
            BASE64Encoder base64Encoder = new BASE64Encoder();
            return base64Encoder.encode(file.getBytes());
        }catch (Exception e){
            log.error("图片转换异常", e);
            throw new AdminException("图片转换异常！");
        }

    }
}
